<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    width="63%"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="名称" prop="name"
                :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属站点" prop="siteId"
                        :rules="[
                          {required: true, message:'站点不能为空', trigger:'blur'}
                 ]">
            <el-select v-model="inputForm.siteId" placeholder="请选择站点">
              <el-option
                v-for="item in siteList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模型" prop="model.id"
                        :rules="[
                          {required: true, message:'关联模型不能为空', trigger:'blur'}
                 ]">
            <el-select v-model="inputForm.model.id" placeholder="请选择模型">
              <el-option
                v-for="item in modelList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="图标" prop="image"
                :rules="[
                 ]">
              <el-input v-model="inputForm.image" clearable @focus="selectIcon" placeholder="菜单图标名称"></el-input>

           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="链接" prop="href"
                :rules="[
                  {validator: validator.isURL, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.href" placeholder="请填写链接"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="关键字" prop="keywords"
                :rules="[
                 ]">
              <el-input v-model="inputForm.keywords" placeholder="请填写关键字"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="描述" prop="description"
                :rules="[
                 ]">
              <el-input v-model="inputForm.description" placeholder="请填写描述"     ></el-input>
           </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="排序" prop="sort"
                        :rules="[
                 ]">
            <el-input-number size="small" v-model="inputForm.sort"  :min="0"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用" prop="enableFlag"
                        :rules="[
                 ]">
            <el-switch
                v-model="inputForm.enableFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="首页显示" prop="homeFlag"
                        :rules="[
                 ]">
            <el-switch
                v-model="inputForm.homeFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="允许评论" prop="commentFlag"
                        :rules="[
                 ]">
            <el-switch
                v-model="inputForm.commentFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需要审核" prop="auditFlag"
                        :rules="[
                 ]">
            <el-switch
                v-model="inputForm.auditFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注信息" prop="remarks">
            <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
          </el-form-item>
        </el-col>

        <el-col v-if="method!=='view'">
          <el-tag type="info">参数配置</el-tag>
          <vue-json-editor
            v-if="method!=='view'"
            ref="paramConfigEditor"
            :show-btns="false"
            v-model="inputForm.jsonParam"
            :mode="'code'"
            lang="zh"
            @json-change="onJsonChange">
          </vue-json-editor>
        </el-col>
        <el-col :span="12" v-else>
          <el-form-item label="参数配置" prop="paramConfig"
                        :rules="[
                 ]">
            <el-input type="textarea" v-model="inputForm.paramConfig" placeholder="请填写参数配置"></el-input>

          </el-form-item>
        </el-col>

        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
  <Icon ref="icon" @getValue="value => inputForm.image = value"></Icon>
</div>
</template>

<script>
import Icon from '@/components/icon'
import SelectTree from '@/components/treeSelect/treeSelect.vue'
import vueJsonEditor from 'vue-json-editor'

export default {
  props: {
    siteList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        name: '',
        parent: {
          id: '',
          name: ''
        },
        sort: '',
        model: {
          id: ''
        },
        siteId: '',
        image: '',
        href: '',
        enableFlag: 1,
        homeFlag: '',
        commentFlag: '',
        auditFlag: '',
        keywords: '',
        description: '',
        paramConfig: '',
        hits: '',
        remarks: ''
      },
      modelList: []
    }
  },
  components: {
    Icon,
    SelectTree,
    vueJsonEditor
  },
  watch: {
    'inputForm.siteId': {
      handler (newVal, oldVal) {
        if (!!newVal && !!oldVal && newVal !== oldVal && this.inputForm.model) {
          this.inputForm.model.id = ''
        }

        if (newVal) {
          let url = '/cms/model/cmsModel/allList'
          this.$http({
            url: url,
            method: 'get',
            params: {
              enableFlag: '1',
              siteId: newVal
            }
          }).then(({data}) => {
            this.modelList = data.data
          })
        }
      },
      immediate: true,
      deep: false
    }
  },
  methods: {
    init (method, obj) {
      this.method = method
      this.inputForm.id = obj.id
      if (method === 'add') {
        this.title = '新建栏目'
      } else if (method === 'addChild') {
        this.title = '添加下级栏目'
      } else if (method === 'edit') {
        this.title = '修改栏目'
      } else if (method === 'view') {
        this.title = '查看栏目'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        this.inputForm.parent.id = obj.parent.id
        this.inputForm.parent.name = obj.parent.name
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/cms/column/cmsColumn/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.cmsColumn)
            this.loading = false
          })
        } else {
          this.inputForm.siteId = obj.siteId
        }
      })
    },
      // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/cms/column/cmsColumn/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    },
    selectIcon () {
      this.$refs.icon.visible = true
    },
    onJsonChange (value) {
      this.inputForm.paramConfig = JSON.stringify(value)
    }
  }
}
</script>
